﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Guess
{
    public partial class GuessInfo : System.Web.UI.Page
    {
        protected int userID = 28;//暂时测试用户28
        protected int guessID;//测试竞猜ID2
        protected string guessState = "";//竞猜状态
        protected int guesstype;

        protected void Page_Load(object sender, EventArgs e)
        {
            string struserIDFromCookie = Common.Cookie.getCookie("userID");
            if (string.IsNullOrEmpty(struserIDFromCookie))
            {
               // Response.Redirect("/");
            }
            else
            {
                userID = int.Parse(struserIDFromCookie);
            }

            if (!string.IsNullOrEmpty(Request.QueryString["GuessID"]))
            {
                guessID = Convert.ToInt32(Request.QueryString["GuessID"]);
            }
            else
            {
                guessID = Convert.ToInt32(Request["hidReviewID"]);
            }

            //删除竞猜
            if (!string.IsNullOrEmpty(Request.QueryString["s"]) && Request.QueryString["s"].Equals("del"))  //判断是否要删除
            {
                Jy123.Model.User_Main user = new BLL.User_Main().GetModel(userID);
                Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(guessID);

                new BLL.Public.State().CreateGuessState(user, null, guess, 5);//添加删除动态

                if (guess.FType == 2)
                {
                    user.Cash = user.Cash + guess.SumPoint;
                    new BLL.User_Main().Update(user);//如果是精彩类型2返回预付的现金
                }
                if (new BLL.Guess.GuessSql().DeleteByWhere("user_guess_main", "id =" + guessID) > 0)
                {
                   
                    new BLL.Guess.GuessSql().DeleteByWhere("user_guess_item", "fguess =" + guessID);
                    new BLL.Guess.GuessSql().DeleteByWhere("user_guess_review", "fguess =" + guessID);
                    new BLL.Guess.GuessSql().DeleteByWhere("user_guess_stat", "fguess =" + guessID);
                }
                Response.Redirect("/Guess/GuessList.aspx?type=MyCreate");//提交成功跳转到我的竞猜页
            }

            //删除回复
            if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"].Equals("redel"))
            {
                int rid = Convert.ToInt32(Request.QueryString["rid"]);
                new BLL.User_Guess_Review().Delete(rid);
            }

            //加码竞猜
            if (!string.IsNullOrEmpty(Request.QueryString["s"]) && Request.QueryString["s"].Equals("add"))  //判断是否要加码
            {
                Jy123.Model.User_Main user = new BLL.User_Main().GetModel(userID);
                Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(guessID);

                if (user.Cash < guess.Point && DateTime.Compare(DateTime.Now, guess.OverTime) < 0)//判断现金是否足够,是否在竞猜时间内
                {
                    Response.Redirect("/Guess/GuessInfo.aspx?GuessID=" + guessID);
                }
                else
                {
                    int itemID = Convert.ToInt32(Request.QueryString["ItemID"]);
                    Jy123.Model.User_Guess_Stat stat = new Jy123.Model.User_Guess_Stat();

                    stat.FGuess = guessID;
                    stat.FUser = userID;
                    stat.FItem = itemID;
                    stat.Point = guess.Point;
                    stat.Time = DateTime.Now;

                    if (new BLL.User_Guess_Stat().Add(stat) > 0)
                    {
                        user.Cash = user.Cash - guess.Point;

                        new BLL.User_Main().Update(user); //扣去用户参加竞猜所需的现金

                        Jy123.Model.User_Main friend = new BLL.User_Main().GetModel(guess.FUser);

                        new BLL.Public.State().CreateGuessState(user, friend, guess, 3); //添加加码竞猜动态

                        Response.Redirect("/Guess/GuessInfo.aspx?GuessID=" + guessID);
                    }
                }
            }

            //撤销竞猜
            if (!string.IsNullOrEmpty(Request.QueryString["s"]) && Request.QueryString["s"].Equals("clear"))  //判断是否要撤销
            {
                Jy123.Model.User_Main user = new BLL.User_Main().GetModel(userID);
                Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(guessID);

                int i = new BLL.Guess.GuessInfo().StatClear(userID,guessID);

                if ( i > 0)
                {
                    int GuessCount = new BLL.Guess.GuessInfo().getGuessCount(userID,guessID);
                    user.Cash = user.Cash + guess.Point*GuessCount;

                    new BLL.User_Main().Update(user); //返回用户参加竞猜所需的现金

                    Jy123.Model.User_Main friend = new BLL.User_Main().GetModel(guess.FUser);

                    new BLL.Public.State().CreateGuessState(user, friend, guess, 4); //添加撤销竞猜动态
                }
            }

            //提交回复
            string strSubmit = Request["btnSubmit"];
            if (!string.IsNullOrEmpty(strSubmit))
            {
                Jy123.Model.User_Guess_Review review = new Jy123.Model.User_Guess_Review();

                review.FGuess = guessID;
                review.FUser = userID;
                review.RContent = Request["txtComment"];
                review.Time = DateTime.Now;

                int i = new BLL.User_Guess_Review().Add(review);
            }
            PageLoad();//测试
            
        }

        /// <summary>
        /// 获取页面
        /// </summary>
        protected void PageLoad()
        {
            Model.User_Main userModel = new BLL.User_Main().GetModel(userID);

            StringBuilder sbHtml = new StringBuilder(Cache["guess_index"].ToString());//主sb

            //公共部分
            sbHtml = sbHtml.Replace("$Javascript$", Cache["guess_js"].ToString() + Cache["guess_infojs"].ToString());//JS
            sbHtml = sbHtml.Replace("$GuessID$", guessID.ToString());//JS替换竞猜ID
            sbHtml = sbHtml.Replace("$UserID$", userID.ToString());//JS替换用户ID
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友
            sbHtml = sbHtml.Replace("$GuessLogo$", Cache["guess_logo"].ToString());//竞猜LOGO
            sbHtml = sbHtml.Replace("$GuessMenu$", Cache["guess_menu"].ToString());//竞猜菜单

            sbHtml = sbHtml.Replace("$IndexClass$", "")
               .Replace("$AddClass$", "")
               .Replace("$MyFriendClass$", "")
               .Replace("$MyCreateClass$", "")
               .Replace("$MyJoinClass$", "")
               .Replace("$TopClass$", "class=\"last\"")
               .Replace("$Cash$", Convert.ToInt32(userModel.Cash).ToString("n0"));
                
            sbHtml = sbHtml.Replace("$Content$",getGuessInfo());//竞猜列表

            sbHtml = sbHtml.Replace("$GuessItem$", getGuessItem());//竞猜项

            if (guessState.Equals("Unaudited"))
            {
                sbHtml = sbHtml.Replace("$UserStatLi$", "");//用户竞猜统计

                sbHtml = sbHtml.Replace("$GuessReview$", "");//竞猜回复

                sbHtml = sbHtml.Replace("$UserGuessDynamicLi$","");//用户竞猜统计
            }
            else
            {
                sbHtml = sbHtml.Replace("$UserStatLi$", getUserGuessStat());//用户竞猜统计

                sbHtml = sbHtml.Replace("$UserGuessDynamicLi$", getGuessDynamic());//竞猜动态

                sbHtml = sbHtml.Replace("$GuessReview$", Cache["guess_review"].ToString());//竞猜回复

                sbHtml = sbHtml.Replace("$hidReviewID$",guessID.ToString());//隐藏域竞猜ID

                sbHtml = sbHtml.Replace("$GuessReviewListLi$", getReviewListLi());//竞猜回复列表

                sbHtml = sbHtml.Replace("$FaceList$", new BLL.User.UserSql().FaceList(Cache["user_face"].ToString()));//表情列表
            }
            Response.Write(sbHtml.ToString());

        }

        /// <summary>
        /// 获取竞猜信息HTML
        /// </summary>
        /// <param name="id">竞猜ID</param>
        /// <returns>竞猜信息HTML</returns>
        protected string getGuessInfo()
        {
            StringBuilder sbHtml = new StringBuilder(Cache["guess_info"].ToString());//主sb

            Model.User_Guess_Main guessModel = new BLL.User_Guess_Main().GetModel(guessID);
            BLL.Guess.GuessList guesslistBll = new Jy123.BLL.Guess.GuessList();



            //这是4种状态样式 oOpen oDone oClose,oUnaudited
            if (!guessModel.IsSettlement && DateTime.Compare(DateTime.Now, guessModel.OverTime) > 0 && guessModel.IsCheckup)
            {
                sbHtml = sbHtml.Replace("$GuessInfoClass$", "oClose")  //竞猜截止
                    .Replace("$OverTime$", guessModel.OverTime.ToString("MM月dd日 HH:mm"))
                    .Replace("$OpenTime$", guessModel.OpenTime.ToString("MM月dd日 HH:mm"));
                guessState = "Close";
            }
            else if (DateTime.Compare(DateTime.Now, guessModel.OpenTime) > 0 && guessModel.IsCheckup && guessModel.IsSettlement)
            {
                sbHtml = sbHtml.Replace("$GuessInfoClass$", "oDone")  //已结束
                    .Replace("$OverTime$", guessModel.OverTime.ToString("MM月dd日 HH:mm"))
                    .Replace("$OpenTime$", guessModel.OpenTime.ToString("MM月dd日 HH:mm"));
                guessState = "Done";
            }
            else if (DateTime.Compare(DateTime.Now, guessModel.OverTime) < 0 && guessModel.IsCheckup)
            {
                sbHtml = sbHtml.Replace("$GuessInfoClass$", "oOpen")    //竞猜中
                    .Replace("$OverTime$", guessModel.OverTime.ToString("MM月dd日 HH:mm"))
                    .Replace("$OpenTime$", guessModel.OpenTime.ToString("MM月dd日 HH:mm"));
                guessState = "Open";
            }
            else if (!guessModel.IsCheckup)
            {
                sbHtml = sbHtml.Replace("$GuessInfoClass$", "oUnaudited")    //未审核
                    .Replace("$GuessInfoButtonLi$", "")//竞猜按钮
                    .Replace("$GuessCount$", "")
                    .Replace("$OverTime$", "")
                    .Replace("$OpenTime$", "");
                guessState = "Unaudited";
            }
            if (!new BLL.Guess.GuessInfo().ExistsGuessStat(guessModel.ID, userID))//用户是否进行过竞猜
            {
                if ((userID == guessModel.FUser && guessModel.FType == 1) || userID != guessModel.FUser) //本人发帖并且是竞猜类型1 
                {
                    sbHtml = sbHtml.Replace("$GuessInfoButtonLi$", Cache["guess_infobuttonli"].ToString());//竞猜按钮
                }
                else
                {
                    sbHtml = sbHtml.Replace("$GuessInfoButtonLi$", "");
                }
            }
            else
            {
                sbHtml = sbHtml.Replace("$GuessInfoButtonLi$", "");
            }

            sbHtml = sbHtml.Replace("$GuessPoint$", guessModel.Point.ToString())
                .Replace("$GuessCount$", new BLL.Guess.GuessSql().GetStatHtml(guessModel.ID))
                .Replace("$GuessMultiple$", guesslistBll.getGuessMultiple(guessModel.Multiple))
                .Replace("$GuessTitle$", guessModel.Title);

            //是否本人发并且未审核起 添加删除按钮
            if ((userID == guessModel.FUser && guessState.Equals("Unaudited")) || (userID == guessModel.FUser && guessState.Equals("Unaudited")))
            {
                sbHtml = sbHtml.Replace("$DelLi$", Cache["guess_infodelbuttonli"].ToString());
                sbHtml = sbHtml.Replace("$GuessID$", guessModel.ID.ToString());
            }
            else
            {
                sbHtml = sbHtml.Replace("$DelLi$", "");
            }

            return sbHtml.ToString();    
        }

        /// <summary>
        /// 获取竞猜项HTML
        /// </summary>
        /// <returns>竞猜项HTML</returns>
        protected string getGuessItem()
        {
            StringBuilder sbHtml = new StringBuilder();

            string strLi = Cache["guess_itemli"].ToString();//竞猜项LI

            List<Jy123.Model.User_Guess_Item> itemList = new BLL.User_Guess_Item().GetModelList("FGuess =" + guessID);

            foreach (Jy123.Model.User_Guess_Item itemModel in itemList)
            {
                string str = strLi;
                if (guessState.Equals("Open"))   //竞猜中
                {
                    str = str.Replace("$InputHtml$", Cache["guess_itemradio"].ToString())
                        .Replace("$GuessItemID$", itemModel.ID.ToString());
                    str = str.Replace("$disabled$", "");
                    str = str.Replace("$GuessItemName$", itemModel.Item);

                }
                else if (guessState.Equals("Done"))    //已结束          
                {
                    if (new BLL.Guess.GuessSql().CheckRight(itemModel.ID))
                    {
                        str = str.Replace("$InputHtml$", Cache["guess_rightgif"].ToString());
                        str = str.Replace("$GuessItemName$", "<b>" + itemModel.Item + "</b>");
                    }
                    else
                    {
                        str = str.Replace("$InputHtml$", Cache["guess_itemradio"].ToString());
                        str = str.Replace("$disabled$", "disabled=\"disabled\"");
                        str = str.Replace("$GuessItemName$", itemModel.Item);
                    }
                }
                else if (guessState.Equals("Close"))    //已截止
                {
                    str = str.Replace("$InputHtml$", Cache["guess_itemradio"].ToString())
                        .Replace("$GuessItemID$", itemModel.ID.ToString());
                    str = str.Replace("$disabled$", "disabled=\"disabled\"");
                    str = str.Replace("$GuessItemName$", itemModel.Item);
                }
                else if (guessState.Equals("Unaudited"))    //未审核
                {
                    if (new BLL.Guess.GuessSql().CheckRight(itemModel.ID))
                    {
                        str = str.Replace("$InputHtml$", Cache["guess_rightgif"].ToString());
                        str = str.Replace("$GuessItemName$", "<b>" + itemModel.Item + "</b>");
                    }
                    else
                    {
                        str = str.Replace("$InputHtml$", Cache["guess_itemradio"].ToString());
                        str = str.Replace("$disabled$", "disabled=\"disabled\"");
                        str = str.Replace("$GuessItemName$", itemModel.Item);
                    }
                }


                if (!guessState.Equals("Unaudited"))//是否为未审核
                {
                    str = str.Replace("$GuessItemCount$", new BLL.Guess.GuessInfo().getGuessItemStat(itemModel.ID));//竞猜项统计
                }
                else
                {
                    str = str.Replace("$GuessItemCount$", "");
                }
                sbHtml.Append(str);
            }

            return sbHtml.ToString();
            
        }

        /// <summary>
        /// 获取用户竞猜统计
        /// </summary>
        /// <returns>用户竞猜统计HTML</returns>
        protected string getUserGuessStat()
        {
            if (new BLL.Guess.GuessInfo().getGuessCount(userID,guessID) == 0)
            {
                return "";
            }
            StringBuilder sbHtml = new StringBuilder(Cache["guess_userstatli"].ToString());

            Jy123.Model.User_StatInfo statinfo = new BLL.Guess.GuessInfo().getUserGuesssStat(userID, guessID);
   
            sbHtml.Replace("$UserGuessCount$", statinfo.Count.ToString())
                .Replace("$UserGuessItem$", statinfo.Item);
            if (guessState == "Open")
            {

                string str;
                if (statinfo.IsMax)//判断是否已经达到最大竞猜次数
                {
                    str = "<dd class=\"gbbody\"><a href=\"GuessInfo.aspx?s=clear&GuessID=" + guessID + "&ItemID=" + statinfo.ItemID + "\">撤销</a></dd>";
                }
                else
                {
                    str = "<dd class=\"gbbody\"><a href=\"GuessInfo.aspx?s=add&GuessID=" + guessID + "&ItemID=" + statinfo.ItemID + "\">加码</a><a href=\"GuessInfo.aspx?s=clear&GuessID=" + guessID + "&ItemID=" + statinfo.ItemID + "\">撤销</a></dd>";
                }
                sbHtml.Replace("$ddAC$", str);//如果竞猜中添加竞猜和撤销按钮
            }
            else
            {
                sbHtml.Replace("$ddAC$", "");
            }
            return sbHtml.ToString();
        }

        /// <summary>
        /// 获取竞猜动态
        /// </summary>
        /// <returns>竞猜动态HTML</returns>
        protected string getGuessDynamic()
        {
            StringBuilder sbWhere = new StringBuilder();

            string strWhere = "fguess = {0} order by Time desc";

            sbWhere.AppendFormat(strWhere, guessID);

            List<Jy123.Model.User_Guess_Stat> GuessStatList = new BLL.User_Guess_Stat().GetModelList(sbWhere.ToString());

            StringBuilder sbHtml = new StringBuilder();

            string strLi = Cache["guess_userdynamicli"].ToString();

            foreach (Jy123.Model.User_Guess_Stat statModel in GuessStatList)
            {
                Model.User_Main userModel = new BLL.User_Main().GetModel(statModel.FUser);

                string str = strLi;

                str = str.Replace("$GuessUserName$", userModel.UserName)
                    .Replace("$GuessTime$", statModel.Time.ToString("MM月dd日 HH:mm"))
                    .Replace("$GuessDynamic$", getGuessDynamic(statModel));

                sbHtml.Append(str);
            }

            return sbHtml.ToString();
        }

        /// <summary>
        /// 获取竞猜动态类型
        /// </summary>
        /// <param name="Point">竞猜统计Model</param>
        /// <returns>竞猜动态String</returns>
        protected string getGuessDynamic(Jy123.Model.User_Guess_Stat statModel)
        {
            StringBuilder sb = new StringBuilder();

            string str = "";
            if (new BLL.Guess.GuessInfo().isFristGuess(statModel))
            {
                str = "{0} 竞猜 \"{1}\"";
            }
            else 
            {
                str = "{0} 加码 \"{1}\"";
            }

            sb.AppendFormat(str, statModel.Time.ToString("MM月dd日 HH:mm"), new BLL.Guess.GuessInfo().getItem(statModel.FItem));

            return sb.ToString();
        }


        /// <summary>
        /// 获取竞猜回复LI
        /// </summary>
        /// <returns>竞猜回复LIHTML</returns>
        protected string getReviewListLi()
        {
            StringBuilder sbHtml = new StringBuilder();

            string strli = Cache["guess_reviewlistli"].ToString();

            List<Jy123.Model.User_Guess_Review> modelList= new BLL.Guess.GuessInfo().getReviewList(guessID);

            foreach (Jy123.Model.User_Guess_Review review in modelList)
            {

                Jy123.Model.User_Main user = new BLL.User_Main().GetModel(review.FUser);
                string str = strli;

                str = str.Replace("$GuessReviewTime$", review.Time.ToString("MM月dd日 HH:mm"));
                if (review.FUser == userID)
                {
                    str = str.Replace("$DelImg$", Cache["guess_reviewdelimg"].ToString());
                    str = str.Replace("$ReviewID$", review.ID.ToString());
                    str = str.Replace("$GuessID$", guessID.ToString());
                }
                else
                {
                    str = str.Replace("$DelImg$", "");
                }
                str = str.Replace("$ReviewUserID$", review.FUser.ToString());
                str = str.Replace("$ReviewUserName$", user.UserName);
                str = str.Replace("$ReviewContent$", BLL.User.UserSql.BlogContent(review.RContent));

                sbHtml.Append(str);
            }

            return sbHtml.ToString();

        }
    }
}
